﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
	/// <summary>
	/// 数据访问类:DetectVariety
	/// </summary>
	public partial class DetectVariety
	{
		public DetectVariety()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return SQLiteHelper.GetMaxID("itemTypeID", "DetectVariety"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int itemTypeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from DetectVariety");
			strSql.Append(" where itemTypeID=@itemTypeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@itemTypeID", DbType.Int32,4)			};
			parameters[0].Value = itemTypeID;

			return SQLiteHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.DetectVariety model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into DetectVariety(");
			strSql.Append("itemTypeID,typeName,OrderNumber,IsShow)");
			strSql.Append(" values (");
			strSql.Append("@itemTypeID,@typeName,@OrderNumber,@IsShow)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@itemTypeID", DbType.Int32,4),
					new SQLiteParameter("@typeName", DbType.String,20),
					new SQLiteParameter("@OrderNumber", DbType.Int32,4),
					new SQLiteParameter("@IsShow", DbType.Boolean,1)};
			parameters[0].Value = model.itemTypeID;
			parameters[1].Value = model.typeName;
			parameters[2].Value = model.OrderNumber;
			parameters[3].Value = model.IsShow;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.DetectVariety model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update DetectVariety set ");
			strSql.Append("typeName=@typeName,");
			strSql.Append("OrderNumber=@OrderNumber,");
			strSql.Append("IsShow=@IsShow");
			strSql.Append(" where itemTypeID=@itemTypeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@typeName", DbType.String,20),
					new SQLiteParameter("@OrderNumber", DbType.Int32,4),
					new SQLiteParameter("@IsShow", DbType.Boolean,1),
					new SQLiteParameter("@itemTypeID", DbType.Int32,4)};
			parameters[0].Value = model.typeName;
			parameters[1].Value = model.OrderNumber;
			parameters[2].Value = model.IsShow;
			parameters[3].Value = model.itemTypeID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int itemTypeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DetectVariety ");
			strSql.Append(" where itemTypeID=@itemTypeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@itemTypeID", DbType.Int32,4)			};
			parameters[0].Value = itemTypeID;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string itemTypeIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DetectVariety ");
			strSql.Append(" where itemTypeID in ("+itemTypeIDlist + ")  ");
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DetectVariety GetModel(int itemTypeID)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select itemTypeID,typeName,OrderNumber,IsShow from DetectVariety ");
            strSql.Append(" where itemTypeID=@itemTypeID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@itemTypeID", DbType.Int32,4)			};
			parameters[0].Value = itemTypeID;

			Clipper.Model.DetectVariety model=new Clipper.Model.DetectVariety();
			DataSet ds=SQLiteHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DetectVariety DataRowToModel(DataRow row)
		{
			Clipper.Model.DetectVariety model=new Clipper.Model.DetectVariety();
			if (row != null)
			{
				if(row["itemTypeID"]!=null && row["itemTypeID"].ToString()!="")
				{
					model.itemTypeID=int.Parse(row["itemTypeID"].ToString());
				}
				if(row["typeName"]!=null)
				{
					model.typeName=row["typeName"].ToString();
				}
                if (row["OrderNumber"] != null && row["OrderNumber"].ToString() != "")
                {
                    model.OrderNumber = int.Parse(row["OrderNumber"].ToString());
                }
                if (row["IsShow"] != null && row["IsShow"].ToString() != "")
                {
                    if ((row["IsShow"].ToString() == "1") || (row["IsShow"].ToString().ToLower() == "true"))
                    {
                        model.IsShow = true;
                    }
                    else
                    {
                        model.IsShow = false;
                    }
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select itemTypeID,typeName,OrderNumber,IsShow ");
			strSql.Append(" FROM DetectVariety ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM DetectVariety ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SQLiteHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.itemTypeID desc");
			}
			strSql.Append(")AS Row, T.*  from DetectVariety T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SQLiteHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "DetectVariety";
			parameters[1].Value = "itemTypeID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 获得导出数据列表，只导出itemTypeID,typeName列
        /// </summary>
        public DataSet GetExportList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select itemTypeID,typeName ");
            strSql.Append(" FROM DetectVariety ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteHelper.Query(strSql.ToString());
        }

		#endregion  ExtensionMethod
	}
}

